<?php


namespace App\Http\Controllers\Web;


use App\Http\Controllers\Controller;
use App\Models\Product;
use App\Models\Team;
use App\Repositories\TeamRepository;
use Illuminate\Http\Request;

class TeamController extends Controller
{
    public function detail($id){
        $team = Team::where('id',$id)->where('status',1)->first();
        if(!$team){
            abort(404);
        }
        return view('web.team-detail',compact('team'));
    }

    public function product($id,Request $request){

        $team = Team::where('id',$id)->where('status',1)->first();

        if(!$team){
            abort(404);
        }

        $products = Product::with('font')->where('status',1)->where('team_id',$id)->where('show_video',0);
        $queue_title = '新妹優先';
        if($request->has('order')){
            $order = $request->get('order');
            if($order == 'red'){
                $products = $products->orderBy('price','desc');
                $queue_title = '紅牌優先';
            }else if($order == 'secretly'){
                $products = $products->orderBy('is_secretly','desc');
                $queue_title = '暗配優先';
            }else if($order == 'boobs'){
                $products = $products->orderBy('cup','desc');
                $queue_title = '巨乳優先';
            }else if($order == 'long'){
                $products = $products->orderBy('height','desc');
                $queue_title = '長腿優先';
            }else if($order == 'asc'){
                $products = $products->orderBy('height','desc');
                $queue_title = '小隻馬優先';
            }


        }


        $page = $request->query('page');

        $videoProduct = Product::with('font')->where('status', 1)->where('team_id',$id)->where('show_video', 1)->offset($page-1)->limit(1)->get();

        $products = $products->paginate(9);



        if($request->ajax()){
            //sleep(1);
            return view('web.product.ajax-goods',compact('products','videoProduct'));
        }

        $teams = app(TeamRepository::class)->get();
        return view('web.product.index',compact('team','products','teams','queue_title','videoProduct'));
    }

    public function about($id){
        $team = Team::where('id',$id)->where('status',1)->first();

        if(!$team){
            abort(404);
        }
        return view('web.team-about',compact('team'));
    }
}
